using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_PetPabilityAttribute : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_PetPabilityAttribute
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExImage frame { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage imgIcon { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textValue { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textName { protected set; get; }
            public UnityEngine.GameObject state { protected set; get; }
            public UnityEngine.GameObject lockState { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("frame", out var __tbv0);
                this.frame = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("imgIcon", out var __tbv1);
                this.imgIcon = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textValue", out var __tbv2);
                this.textValue = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textName", out var __tbv3);
                this.textName = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("state", out var __tbv4);
                this.state = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("lockState", out var __tbv5);
                this.lockState = __tbv5;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private ListScrollAdapter<UIN_CommonNoElementCell> states;

        private UIPetSlotType slotType = UIPetSlotType.LockByLv;
        private CfgPabilityAttribute cfgPabilityAttribute = null;
        private int attrCid = 0;
        private int attrVal = 0;
        private CfgAttribute cfgAttribute = null;
        private int currIndex = -1;
        private System.Action<int> callOnClick = null;
        #endregion fields

        #region properties

        protected UIB_UIN_PetPabilityAttribute ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_PetPabilityAttribute();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.states = this.InitListScroll<UIN_CommonNoElementCell>(this.ui.state, this.OnStateChanged);
            var exBtn = this.gameObject.GetOrAdd<ExButton>();
            exBtn.onClick.AddListener(this.OnClickSelf);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.ClearData();
        }

        #region methonds

        public void Show(int pabilityAttributeCid, UIPetSlotType slotType, int index, System.Action<int> funcClickSelf)
        {
            this.SetData(pabilityAttributeCid, slotType, index, funcClickSelf);
            this.Show();
            this.ShowStates();
            this.ShowAttributeDetails();
            this.ShowLockState();
            this.ShowFrame();
        }

        private void SetData(int pabilityAttributeCid, UIPetSlotType slotType, int index, System.Action<int> funcClickSelf)
        {
            this.cfgPabilityAttribute = TableCenter.pabilityAttribute.Get(pabilityAttributeCid);
            this.slotType = slotType;
            this.currIndex = index;
            this.callOnClick = funcClickSelf;

            this.InitializeCfgAttribute();
        }

        private void InitializeCfgAttribute()
        {
            int indexAttr = 0;
            this.attrCid = this.cfgPabilityAttribute.AttType[indexAttr];
            this.attrVal = this.cfgPabilityAttribute.AttValve[indexAttr];
            this.cfgAttribute = TableCenter.attribute.Get(attrCid);
        }

        private void ClearData()
        {
            this.cfgPabilityAttribute = null;
            this.cfgAttribute = null;
            this.slotType = UIPetSlotType.LockByLv;
        }

        private void ShowStates()
        {
            this.states.RefillCells(cfgPabilityAttribute.PetSlotCost);
        }

        private void OnStateChanged(UIN_CommonNoElementCell item, int index)
        {
            item.Show();
        }

        private void ShowAttributeDetails()
        {
            string strVal = APIAttributeUIUtils.GetStrAttributeValue(cfgAttribute.ShowType, attrVal);
            this.ui.textName.text = cfgAttribute.Name;
            this.ui.textValue.text = strVal;

            bool isUsed = this.slotType == UIPetSlotType.Used;
            int index = isUsed ? 1 : 0;
            //this.ui.textName.indexSwitchColor = index;
            //this.ui.textValue.indexSwitchColor = index;

            string strIcon = isUsed ? cfgAttribute.IconWhite_fullPath : cfgAttribute.Icon_fullPath;
            this.SetImageSprite(this.ui.imgIcon, strIcon);
        }

        private void ShowLockState()
        {
            this.ui.lockState.SetActive(this.slotType == UIPetSlotType.LockByLv);
        }

        private void ShowFrame()
        {
            //this.ui.frame.indexSwitchSprite = (this.slotType == UIPetSlotType.Used) ? 1 : 0;
        }

        private void OnClickSelf()
        {
            this.ExcuteCall();
        }

        private void ExcuteCall()
        {
            this.callOnClick?.Invoke(this.currIndex);
        }
        #endregion methonds
    }
}
